package com.qianseit.westore.activity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;

import com.beautymiracle.androidclient.R;
import com.qianseit.westore.BaseDoFragment;
import com.qianseit.westore.DoActivity;
import com.qianseit.westore.Run;
import com.qianseit.westore.http.JsonRequestBean;
import com.qianseit.westore.http.JsonTask;
import com.qianseit.westore.http.JsonTaskHandler;
import com.qianseit.westore.ui.CustomDialog;

/**
 * 关于、帮助
 * 
 * 
 */
public class AboutFragment extends BaseDoFragment implements OnClickListener {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mActionBar.setTitle(R.string.about_us);
	}

	@Override
	public void init(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_about, null);

		long newestVCode = Run.loadOptionLong(mActivity,
				Run.pk_newest_version_code, 0);
		if (newestVCode > Run.getVersionCode(mActivity)) {
			mActionBar.setRightTitleButton(R.string.update,
					new OnClickListener() {
						@Override
						public void onClick(View v) {
							Run.excuteJsonTask(new JsonTask(), new UpdateTask(
									(DoActivity) mActivity));
						}
					});
		}
//		WebView webView = (WebView) findViewById(R.id.about_content);
//		InputStream is = null;
//		StringBuilder sb = new StringBuilder();
//		try {
//			is = getResources().getAssets().open("about_us.html");
//			BufferedReader reader = new BufferedReader(new InputStreamReader(is), 16*1024);
//			String line = null;
//			while ((line = reader.readLine()) != null){
//				sb.append(line + "\n");
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally {
//			 try {
//                 is.close();
//             } catch (IOException e) {
//                 e.printStackTrace();
//             }
//		}
//		webView.loadDataWithBaseURL(null, sb.toString(), "text/html", "utf-8", "");
		
//		ImageView imageView = (ImageView) findViewById(R.id.about_app_icon);
//		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.about_us);
//		imageView.setImageBitmap(bitmap);
//		imageView.setImageResource(R.drawable.about_us3);
		// 应用名与版本号
//		PackageManager mPm = mActivity.getPackageManager();
//		try {
//			PackageInfo pi = mPm.getPackageInfo(mActivity.getPackageName(), 0);
//			((TextView) findViewById(R.id.about_version_info))
//					.setText(getString(R.string.about_version, pi.versionName));
//		} catch (NameNotFoundException e) {
//			e.printStackTrace();
//		}
	}

	@Override
	public void onClick(View v) {
		// if (v.getId() == R.id.about_update) {
		// ((DoActivity) mActivity).showCancelableLoadingDialog();
		// new JsonTask().execute(new UpdateTask((DoActivity) mActivity));
		// } else if (v.getId() == R.id.about_license) {
		// }
	}

	public static class UpdateTask implements JsonTaskHandler {
		private DoActivity activity;

		public UpdateTask(DoActivity activity) {
			this.activity = activity;
		}

		@Override
		public JsonRequestBean task_request() {
//			JsonRequestBean jrb = new JsonRequestBean(Run.VERSION_URL, null);
//			jrb.method = JsonRequestBean.METHOD_GET;
			JsonRequestBean jrb = new JsonRequestBean(Run.API_URL,"mobileapi.indexad.get_app_version");
			jrb.addParams("app_name", "shop");
			return jrb;
		}

		@Override
		public void task_response(String json_str) {
			activity.hideLoadingDialog_mt();
			try {
				JSONObject all = new JSONObject(json_str);
				all = all.optJSONObject("data");
				final long version = all.optLong("version_num");
				final String downloadUrl = all.optString("down_url");
				if (version <= Run.getVersionCode(activity)
						|| TextUtils.isEmpty(downloadUrl))
					return;

				// 最新版的code
				Run.savePrefs(activity, Run.pk_newest_version_code, version);

				// 版本提示框
				final CustomDialog dialog = new CustomDialog(activity);
				dialog.setTitle(R.string.update_newest_app);
				dialog.setMessage(all.optString("remark"));
				dialog.setNegativeButton(R.string.cancel, null);
				dialog.setPositiveButton(R.string.ok, new OnClickListener() {
					@Override
					public void onClick(View v) {
						 Run.openBrowser(activity, downloadUrl);
//						 Run.downFile(downloadUrl, version+"" ,activity);
					}
				}).setCancelable(true).show();
			} catch (Exception e) {
			}
		}
	}

}
